var
    body=$("body"),
    c_bg=$("#c_bg"),

    windowMovePara=null,//窗口移动参数
    windowMWU=null,//mouse wheel up
    windowMWD=null,//mouse wheel down
    app=["setting","appStore","music"];

void function ()
{
    var
        o=c_bg,
        c=c_bg[0];
    //初始化图标
    function initIcon() {
        var box=$("#iconBox");
        box.html("");
        for(var i=0;i<app.length;i++)
        {
            var ic=tag.div().mst("w50;h55").attr("title",app[i]);
            box.append(ic);
            var png=tag.div().mst("w50;h50;bgiurl('data/app/"+app[i]+"/images/icon.png');bgs100% 100%;bdr10;cup");
            ic.append(png);
            icClick(png,app[i]);
        }
        function icClick(obj,name)
        {
            obj.bind("click",function () {click(name)})
        }
        function click(name) {
            if(!this.t){
                this.t=_.createElement("SCRIPT");
            }else {
                _.body.removeChild(this.t);
                this.t=_.createElement("SCRIPT");
            }
            this.t.src="js/app/"+name+"/main.js";
            _.body.appendChild(this.t);
        }
    }
    /*log("get apps send start");
    $.get("init.do",function (x) {
        alert(x);
        x=x.split(",");
        app=x;
        initIcon();
    });
    app=["setting"];
    initIcon();
    log("get apps end1");*/
    //ajax is break and i can not fix it


    //做房子
    var houseWin=tag.div().mst("h100%;w50");
    $("body").append(tag.div().mst("poa;r-48;tr300;t0;h100%;w50").hover
    (
        function () {
            $(this).mst("r0")
        },
        function () {
            $(this).mst("r-48")
        }
    )
        .append(houseWin));
    var houseContent=tag.div().attr("id","iconBox");
    window.appHouse=new House(houseContent,houseWin);
    //app=["music"];
    initIcon();
    //拿装饰
    appHouse.pro.mst("dpn");
    appHouse.fix();
    //

    //拿id



    init();
    $(window).bind
    ({
        "mousemove":windowMo,
        "mouseup":windowMU,
        "mousewheel DOMMouseScroll":windowMW
    });

    function init()
    {
        c.width=o.width();
        c.height=o.height();
        $(window).bind("resize",init);
    }
    function windowMo(e)
    {
        if(windowMovePara!==null)
        {
            windowMovePara(e.pageX,e.pageY);
        }
    }
    function windowMU()
    {
        windowMovePara=null;
    }
    function windowMW(e) {
        var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) ||  // chrome & ie
            (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1));              // firefox


        if (delta > 0) {

            windowMWU!==null&&windowMWU();
        } else if (delta < 0) {

            windowMWD!==null&&windowMWD();
        }
    }
}();

function Win()
{
    Win.list=Win.list||[];
    Win.id=Win.id||0;
    Win.id++;
    var window=$("<div class='window' id='window"+Win.id+"'></div>").css("transform","rotateX(90deg)").mst("tr300");

    body.append(window);
    //setTimeout(po () {
    window.css("transform","rotateX(0deg)").mst("l"+(body.width()-1000)/2+";t"+(body.height()-650)/2+";tr0");
    //},100);
    window.bind("mousedown",winMD);
    Win.list.unshift(window);
    for (var i=0;i<Win.list.length;i++)
    {
        Win.list[i].css("z-index", 99 - i)
    }

    return window;
    function winMD(e)
    {
        var className=e.target.className;
        //for(var x in Win.list){
        /*if(Win.list.hasOwnProperty(x)){
        }*/
        for (var i=0;i<Win.list.length;i++) {
            if (Win.list[i].attr("id") === $(this).attr("id")) {
                Win.list[i].css("z-index", "99");
                //老大声明
                Win.showing = Win.list[i];
                //移出队列
                Win.list.splice(i, 1);
                //放到第一位
                Win.list.unshift(Win.showing);
            } else {
                Win.list[i].css("z-index", 98 - i)
            }
        }
        //}
        if(className==="window"||className==="asWindow")
        {
            var
                o=$(this),
                l=e.pageX-$(this).offset().left,
                t=e.pageY-$(this).offset().top,
                ml=body.width(),
                mt=body.height();
                windowMovePara=function (x,y)
            {
                //x轴溢出优化
                if(x>ml+l-4)
                {
                    x=ml+l-4;
                }
                else if(x<-996+l)
                {
                    x=-996+l;
                }
                //y轴溢出优化
                if(y>mt-4+t)
                {
                    y=mt-4+t;
                }else if(y<t)
                {
                    y=t;
                }
                o.css
                ({
                    "left":x-l+"px",
                    "top":y-t+"px"
                })
            };
        }
    }
}
//房子-房间-窗户模型 内容-可视区-进度条控制-鼠标滚轮控制
function House(room, windows) {
    room.mst("poa");
    var view=true;
    function fixPro(percent) {
        pro.css("top",percent*(windows.height()-pro.height())+"px");
    }
    var pro = tag.div().mst("poa;t0;bgrgba(0,0,0,0.3);w2;r0;cud");
    windows.append(room, pro).mst("por;ofh");
    var room_top,pro_top, pro_y_changed, pro_md_y, pro_height, per, room_max_top, room_overflow_height;
    var windowsHeight=windows.height();
    windows.bind
    ({
        "mouseover": function ()
        {
            pro.mst("w10");
            if(!view){
                return;
            }
            windowMWD = function ()
            {
                room_top = room[0].offsetTop;
                if (room_top <= -(room.height() - windows.height()))
                {
                    room.css("top", -(room.height() - windows.height()) + "px");
                } else {
                    room.css("top", room_top - 100 + "px");
                }

                per = room[0].offsetTop/-(room.height()-windows.height());
                per <= 0 && (per = 0);
                fixPro(per);
            };
            windowMWU = function () {
                room_top = room[0].offsetTop;
                if (room_top >= 0) {
                    room.css("top", "0px");
                } else {
                    room.css("top", room_top + 100 + "px");
                }
                per = -room[0].offsetTop/(room.height()-windows.height());
                per > 1 && (per = 1);
                fixPro(per);
            };
        },
        "mouseout": function () {
            pro.mst("w2");
            windowMWD = null;
            windowMWU = null;
        }
    });
    pro.bind
    ({
        "mousedown": function (e) {
            //pt=pro.top();
            pro_md_y = e.pageY;
            pro_top = pro[0].offsetTop;
            pro_height = pro.height();
            room_overflow_height = room.height() - windows.height();
            room_max_top = windowsHeight - pro_height;
            windowMovePara = mouseMoveFun;
        }
    });
    var mouseMoveFun = function (x, y)
    {
        pro_y_changed = y - pro_md_y;
        x = pro_top + pro_y_changed;
        if (x < 0)
        {
            return;
        }
        else if (x > room_max_top)
        {
            //x = room_max_top;
            return;
        }
        per = (x / room_max_top).toFixed(2);
        pro.css("top", x + "px");
        room.css("top", -per * room_overflow_height + "px");
    };
    House.prototype.fix = function ()
    {
        var per = windows.height() / room.height();
        if(per>1){
            view=false;
            pro.mst("dpn");
        }
        pro.css("height", per * windows.height() + "px");
    };
    House.prototype.pro=pro;
}

